<?php
/**
 * Created by PhpStorm.
 * User: Apache
 *  1 展会活动 2 环节
 *
 */

namespace app\api\controller\activemanage;
use app\api\controller\common\ApiBase;
use think\Db;
use think\Model;
use app\api\model\Admin as AdminModel;
use app\api\controller\common\CommonPlus;
use think\Controller; // dengh quxiao
// Controller  ApiBase （dengl）
class Zhactivehuanjie extends Controller
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        //$this->model = model('CompanyPublic');
    }


    /**
     * 根据公海id获得获取
     * @return \think\response\Json|void
     *  http://dxshowxya.for.pub/index.php?s=/api/basemanage/Yuyuemanage/getList
     */

     public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
     }

    

     /**
     *
     * @return \think\response\Json|void
     *  comppub
     *  不知道跟展会环节有啥关联  ？
     */

    public function getList(){
        $page = $this->request->param('page');
        $page = max(1,intval($page));
        $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $exhibition_id =$this->request->param('exhibition_id');
        if(empty($exhibition_id)){
           // return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
         }
        $param=" and m.is_del=0";
        if(!empty($exhibition_id)){
           // $param.=" and m.exhibition_id=$exhibition_id ";
        }
        $keyword =input('keyword');
        if (!empty($keyword)) {
            $param.=" and (e.title like '%$keyword%') ";
        }
        $param.=" order by m.id desc";

        $tabName =$this->getTabName($exhibition_id);
        $sql2="select m.*,e.title as title_hd";
        $sql2.=" from crm_zh_active_huanjie  m ";
        $sql2.=" left join crm_zh_active as e on e.id=m.id_hd";
        //$sql2.=" left join crm_".$tabName." as c on c.id=m.id_zs";
        $sql2.=" where 1=1 $param";
        $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
        $list=Db::query($sql2);
        $sql21=" select count(m.id) as count1 ";
        $sql21.=" from crm_zh_active_huanjie as m";
        $sql21.=" left join crm_zh_active as e on e.id=m.id_hd";
        $sql21.=" where 1=1 $param ";
        $total1=Db::query($sql21);
        $total=$total1[0]['count1']; //st
        // 1
        foreach($list as &$val){
           // $val['pic']=unserialize($val['pic']);
          // $val['time_st']=date('Y-m-d',$val['time_st']);
          // $val['time_ed']=date('Y-m-d',$val['time_ed']);
        }
         unset($val);

        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }


 
    /**
     *  添加 环节
     */
    public function add(){
        $com =new CommonPlus();
        $exhibition_id = $this->request->param('exhibition_id');
        $id_zs = $this->request->param('id_zs');
        if(empty($exhibition_id)){
         //  return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        $data['create_time']=time();
        $data['id_hd']=$this->request->param('id_hd');
        $data['title_huanjie']=$this->request->param('title_huanjie');   // 环节名称 
        $data['file']=$this->request->param('file');  // 文件
        // ---
        $res=$com->insert_com_dan($data, '', 'zh_active_huanjie');
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }


    /**
     * 删除 环节
     */
    public function del(){
        $com =new CommonPlus();
        $id = $this->request->param('id');
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $tabName='zh_active_huanjie';
        $r = db($tabName)->where(['id'=>$id])->find();
        if(!$r){
            return json(['code'=>0,'msg'=>'记录不存在','data'=>'']);
        }

        $where['id'] = $id;
        $data['is_del']=1;
        $r=$com->edit_com_fxlist($data, $where, $tabName);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }
    /**
     * 批量删除环节
     */
    public function delall(){
        $com =new CommonPlus();
        $tabName='zh_active_huanjie';
        $id = $this->request->param('id');
        $ids = explode(',',$id);
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        foreach ($ids as $id){
            $where['id'] = $id;
            $r=$com->sel_com_one('222', $where, $tabName);   

            if(!$r){
                return json(['code'=>0,'msg'=>'id:'.$id.'记录不存在,批量删除失败','data'=>'']);
            }else{
                $where1['id'] = $id;
                $data['is_del']=1;
                $r=$com->edit_com_fxlist($data, $where1, $tabName);
                if(!$r){
                    return json(['code'=>0,'msg'=>'id:'.$id.'删除失败','data'=>'']);
                }
            }
      
            
        }

        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
        
        Db::startTrans();
        try {
           // $map['id'] = array('in',$ids);
           // $r = $this->model->where($map)->delete(); 
           // Db::commit();
           // return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
        } catch (\Exception $e) {
            //如获取到异常信息，对所有表的删、改、写操作，都会回滚至操作前的状态：
           // Db::rollback();
          //  return json(['code'=>0,'msg'=>'操作失败，请重试','data'=>'']);
        }
       

    }


    /**
     * 基本信息修改 环节
     * @return \think \response\Json|void
     */
    public function edit()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        //$create_uid  = $this->admin['id'];
        $data['update_time']=time();
   
       // $data['id_zs']=$id_zs;
        $data['id_hd']=$this->request->param('id_hd');
        $data['title_huanjie']=$this->request->param('title_huanjie');   // 环节名称 
        $data['file']=$this->request->param('file');  // 文件


        $where['id'] = $id;
        $tbname="zh_active_huanjie";
        //$data2=array('pwd'=>md5($pwd),'account'=>'cccccccccc');
        $where2=array('id'=>$id,'company_name'=>'test');
        $r=$com->edit_com_fxlist($data, $where, $tbname);

        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    // 明细 环节
    public function getDetail()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $where['id'] = $id;
        $tbname="zh_active_huanjie";
        $r=$com->sel_com_one('222', $where, $tbname);
        // 
        $tbname1="zh_active";
        $where1['id'] =$r['id_hd'];
        $r1=$com->sel_com_one('222', $where1, $tbname1);
        $r['title']=$r1['title'];  
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }

    


    // the end
    public function export()
    {
        $exhibition_id =$this->request->param('exhibition_id');
        $ids =$this->request->param('ids');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
         }
        $param=" and m.is_del=0";
        if(empty($ids)){
            if(!empty($exhibition_id)){
                $param.=" and m.exhibition_id=$exhibition_id ";
            }
            $keyword =input('keyword');
            if (!empty($keyword)) {
                $param.=" and (c.company_name like '%$keyword%') ";
            }
        }else{
            $param.=" and m.id in ($ids)";
        }
        $tabName =$this->getTabName($exhibition_id);
        $sql2="select m.*,e.title as exhibition_name,c.company_name as company_name_zs,c.id as company_id";
        $sql2.=" from crm_zsgoodszp m ";
        $sql2.=" left join crm_exhibition as e on e.id=m.exhibition_id";
        $sql2.=" left join crm_".$tabName." as c on c.id=m.id_zs";
        $sql2.=" where 1=1 $param";
        $list=Db::query($sql2);
        $columns = array(
            array('title' => '新品中文名称', 'field' => 'title', 'width' => 12),
            array('title' => '新品英文名称', 'field' => 'title_en', 'width' => 12),
            array('title' => '新品报价', 'field' => 'price', 'width' => 12),
            array('title' => '中文介绍', 'field' => 'positions_en', 'width' => 12),
            array('title' => '中文公司', 'field' => 'intro', 'width' => 12),
            array('title' => '英文介绍', 'field' => 'intro_en', 'width' => 12),
            array('title' => '新品照片', 'field' => 'pic', 'width' => 12),
            array('title' => '所属展会', 'field' => 'exhibition_name', 'width' => 12),
            array('title' => '所属展商', 'field' => 'company_name_zs', 'width' => 12),
        );
        $title = "展品明细";
        $filename = $title . date('Y-m-d-H-i', time()) . ".xls";
        return json(['code'=>1,'msg'=>'操作成功','data'=>['columns'=>$columns,'filename'=>$filename,'list'=>$list]]);
    }

        
      //查询活动 前端调用下拉框用
      public function getListForqd(){
        if (request()->isPost()) {
            $where['is_del'] = 0;
            //$where['exhibition_id'] = 0; // 需要区分哪个展会吗？？
            $list = db('zh_active')
            ->where($where)
            ->order('id desc')
            ->select();
            return json(['code'=>1,'msg'=>'success','data'=>$list?$list:array()]);
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }
    // ed-----------

}
